import { _decorator, Label, tween, Tween, UITransform, Widget } from 'cc';
import { Ab_DataTransfer_bA } from '../../../A-FRAME/A-LIB/lib.b.data';
import { Ab_BaseUI_bA } from '../../../A-FRAME/component/ui.base';
import { Ab_ROUND_CONFIG_bA } from '../../game.config';
import { Ab_AudioManager_bA } from '../../manager/audio-manager';
import Ab_GameDC_bA from '../../manager/data.game.center';
import Ab_GameAssets_bA from '../../manager/game.assets';
const { ccclass, property } = _decorator;

@ccclass('Ab_Transition_bA')
export class Ab_Transition_bA extends Ab_BaseUI_bA {
	private Ab_ui_left_bA: UITransform;
	private Ab_ui_right_bA: UITransform;
	private Ab_ui_round_list_bA: UITransform[] = [];

	private Ab_va_left_bA: abi.Ab_cc_bA.Ab_VecAssist_bA;
	private Ab_va_right_bA: abi.Ab_cc_bA.Ab_VecAssist_bA;
	private Ab_va_round_list_bA: abi.Ab_cc_bA.Ab_VecAssist_bA[] = [];

	protected Ab_initAfterOnLoad_bA(){
		this.Ab_ui_left_bA = this.node.getChildByName('bg-left').getComponent(UITransform);
		this.Ab_ui_right_bA = this.node.getChildByName('bg-right').getComponent(UITransform);
		this.Ab_ui_round_list_bA = abi.Ab_array_bA.Ab_create_bA(4, i=>i)
		.map(i=>this.node.getChildByName('round-'+i).getComponent(UITransform))
		.sort((a, b)=>a.node.position.x-b.node.position.x);

		this.Ab_va_left_bA = new abi.Ab_cc_bA.Ab_VecAssist_bA(this.Ab_ui_left_bA.node);
		this.Ab_va_right_bA = new abi.Ab_cc_bA.Ab_VecAssist_bA(this.Ab_ui_right_bA.node);
		this.Ab_va_round_list_bA = this.Ab_ui_round_list_bA.map(ui=>new abi.Ab_cc_bA.Ab_VecAssist_bA(ui.node));

		this.Ab_ui_round_list_bA.forEach(ui=>{
			let wgt = ui.getComponent(Widget);
			if(wgt){
				wgt.enabled = false;
				wgt.destroy();
			}
		});

		this.Ab_renderLocalUI_bA();
	}

	protected start(){
		let wgt = this.node.getComponent(Widget);
		if(wgt) wgt.updateAlignment();

		this.Ab_va_left_bA.Ab_vec_bA.set(this.Ab_va_left_bA.Ab_over_left_bA, this.Ab_ui_left_bA.node.position.y, this.Ab_ui_left_bA.node.position.z);
		this.Ab_va_right_bA.Ab_vec_bA.set(this.Ab_va_right_bA.Ab_over_right_bA, this.Ab_ui_right_bA.node.position.y, this.Ab_ui_right_bA.node.position.z);

		this.Ab_ui_left_bA.node.setPosition(this.Ab_va_left_bA.Ab_vec_bA);
		this.Ab_ui_right_bA.node.setPosition(this.Ab_va_right_bA.Ab_vec_bA);

		this.Ab_ui_round_list_bA.forEach((ui, i)=>{
			let va = this.Ab_va_round_list_bA[i];
			va.Ab_vec_bA.set(ui.node.position.x, va.Ab_over_top_bA, ui.node.position.z);
			ui.node.setPosition(va.Ab_vec_bA);
		});

		this.node.active = false;
	}

	private Ab_enter_bA(duration?: number){
		Ab_AudioManager_bA.Ab_playEffect_bA(Ab_AudioManager_bA.Ab_EAudio_bA.Ab_EF_TRANSITION_bA);
		this.node.active = true;
		duration = duration ?? 0.5;

		let round = Ab_GameDC_bA.Ab_User_bA.Ab_round_bA;
		let long_idx = round - 1;

		this.Ab_ui_round_list_bA.forEach((ui, index)=>{
			let va = this.Ab_va_round_list_bA[index];
			Tween.stopAllByTarget(ui.node);
			va.Ab_vec_bA.x = ui.node.position.x;
			va.Ab_vec_bA.y = va.Ab_over_top_bA;
			ui.node.setPosition(va.Ab_vec_bA);

			let [lock, pass, curr] = [false, false, false];
			if(index < long_idx) [lock, pass, curr] = [false, true, false];
			else if(index === long_idx) [lock, pass, curr] = [false, false, true];
			else [lock, pass, curr] = [true, false, false];

			ui.node.getChildByName('rope-long').active = curr;
			ui.node.getChildByName('rope-short').active = pass || lock;
			ui.node.getChildByName('round-lock').active = lock;
			ui.node.getChildByName('icon-pass').active = pass;
			ui.node.getChildByName('lbl-pass').active = pass;
			ui.node.getChildByName('lbl-round').active = curr;
			ui.node.getChildByName('lbl-ROUND').active = curr;

			va.Ab_vec_bA.x = ui.node.position.x;
			va.Ab_vec_bA.y = va.Ab_top_bA - (curr ? 340 : 280);

			tween(ui.node)
			.to(duration, {position: va.Ab_vec_bA}, {easing: 'cubicOut'})
			.start();
		});

		this.Ab_va_left_bA.Ab_vec_bA.y = this.Ab_ui_left_bA.node.position.y;
		this.Ab_va_right_bA.Ab_vec_bA.y = this.Ab_ui_right_bA.node.position.y;
		this.Ab_va_left_bA.Ab_vec_bA.x = this.Ab_va_left_bA.Ab_over_left_bA;
		this.Ab_va_right_bA.Ab_vec_bA.x = this.Ab_va_right_bA.Ab_over_right_bA;

		[this.Ab_ui_left_bA, this.Ab_ui_right_bA].forEach(ui=>Tween.stopAllByTarget(ui.node));
		this.Ab_ui_left_bA.node.setPosition(this.Ab_va_left_bA.Ab_vec_bA);
		this.Ab_ui_right_bA.node.setPosition(this.Ab_va_right_bA.Ab_vec_bA);

		this.Ab_ui_left_bA.node.getChildByName('label').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(0, round);
		this.Ab_ui_right_bA.node.getChildByName('lbl-multiple').getComponent(Label).string = Ab_ROUND_CONFIG_bA.find(el=>el.round==round).multiple.toString();

		this.Ab_va_left_bA.Ab_vec_bA.x = this.Ab_va_left_bA.Ab_left_bA;
		this.Ab_va_right_bA.Ab_vec_bA.x = this.Ab_va_right_bA.Ab_right_bA;

		tween(this.Ab_ui_left_bA.node)
		.to(duration, {position: this.Ab_va_left_bA.Ab_vec_bA}, {easing: 'cubicOut'})
		.start();

		tween(this.Ab_ui_right_bA.node)
		.to(duration, {position: this.Ab_va_right_bA.Ab_vec_bA}, {easing: 'cubicOut'})
		.start();

		this.scheduleOnce(()=>Ab_GameDC_bA.Ab_Trigger_bA.Ab_UITrans_bA = false, duration + 1.5);
	}

	private Ab_leave_bA(duration?: number){
		duration = duration ?? 0.5;

		let round_idx = Ab_GameDC_bA.Ab_User_bA.Ab_round_bA - 1;

		this.Ab_ui_round_list_bA.forEach((ui, index)=>{
			let va = this.Ab_va_round_list_bA[index];
			va.Ab_vec_bA.x = ui.node.position.x;
			va.Ab_vec_bA.y = va.Ab_over_top_bA;

			Tween.stopAllByTarget(ui.node);

			let tw = tween(ui.node);

			if(index == round_idx) tw = tw.delay(duration * 0.5);

			tw
			.to(duration * 0.5, {position: va.Ab_vec_bA}, {easing: 'cubicOut'})
			.start();
		});

		[this.Ab_ui_left_bA, this.Ab_ui_right_bA].forEach(ui=>Tween.stopAllByTarget(ui.node));

		this.Ab_va_left_bA.Ab_vec_bA.x = this.Ab_va_left_bA.Ab_over_left_bA;
		this.Ab_va_right_bA.Ab_vec_bA.x = this.Ab_va_right_bA.Ab_over_right_bA;
		this.Ab_va_left_bA.Ab_vec_bA.y = this.Ab_ui_left_bA.node.position.y;
		this.Ab_va_right_bA.Ab_vec_bA.y = this.Ab_ui_right_bA.node.position.y;

		tween(this.Ab_ui_left_bA.node)
		.to(duration, {position: this.Ab_va_left_bA.Ab_vec_bA}, {easing: 'cubicOut'})
		.start();

		tween(this.Ab_ui_right_bA.node)
		.to(duration, {position: this.Ab_va_right_bA.Ab_vec_bA}, {easing: 'cubicOut'})
		.call(()=>{
			this.node.active = false;
			Ab_GameDC_bA.Ab_Trigger_bA.Ab_UIGame_bA = true;
			Ab_GameDC_bA.Ab_Trigger_bA.Ab_UIHome_bA = true;
		}).start();
	}

	protected Ab_listening_bA(){
		let list = [];

		list.push(
			Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Trigger_bA, 'Ab_UITrans_bA', (_, show)=>{
				if(_==show) return void 0;
				if(show) this.Ab_enter_bA();
				else this.Ab_leave_bA();
			}, false),
		);

		return list;
	}

	private Ab_renderLocalUI_bA(){
		this.Ab_ui_round_list_bA.forEach((ui, i)=>{
			ui.node.getChildByName('lbl-pass').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(48);
			ui.node.getChildByName('lbl-ROUND').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(47);
			ui.node.getChildByName('lbl-round').getComponent(Label).string = (i+1).toString();
		});

		this.Ab_ui_right_bA.node.getChildByName('lbl-desc').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(46);
		this.Ab_ui_right_bA.node.getChildByName('lbl-MULTIPLE').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(60);

		let ui_icon = this.Ab_ui_right_bA.node.getChildByName('icon-symbol-light-blinblin').getComponent(UITransform);
		let sframe = Ab_GameAssets_bA.Ab_getCounTexture_bA(3);

		let [R, r] = [ui_icon.contentSize, sframe.originalSize].map(s=>s.width/s.height);

		abi.Ab_cc_bA.Ab_skinPeeler_bA(ui_icon.node, sframe, R > r ? {height: true} : {width: true});
	}
}

